﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Legazpi.Payroll.Services.Interfaces;
using Emz.Core.Models;
using System.Data.Entity;
using Emz.Core.Repositories;
using Emz.Core.Base;
using Emz.Core.Models.Enums;

namespace Legazpi.Payroll.Services.Services
{

    public class EmployeeAdjustmentService : IEmployeeAdjustmentService
    {
        #region Fields

        private DbContext _dbContext;
        private IEmployeeAdjustmentRepository _employeeAdjustmentRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public EmployeeAdjustmentService()
            : this(new DatabaseFactory())
        {

        }

        public EmployeeAdjustmentService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _employeeAdjustmentRepository = new EmployeeAdjustmentRepository(_dbContext);
            }
        }

        #endregion

        public EmployeeAdjustment GetById(int id)
        {
            return _employeeAdjustmentRepository.GetById(id);
        }

        public IEnumerable<EmployeeAdjustment> GetAll()
        {
            return _employeeAdjustmentRepository.GetAll();
        }
        
        public IEnumerable<EmployeeAdjustment> GetAllByEmployeeID(int employeeId)
        {
            return GetAll().Where(x => x.EmployeeId == employeeId && x.IsValid);
        }

        public IEnumerable<EmployeeAdjustment> GetAllValidCashAdvanceByEmployeeId(int employeeId)
        {                        
            return _employeeAdjustmentRepository
                .GetMany(x => x.EmployeeId == employeeId 
                    && x.AdjustmentType.AccountType == (int)AccountType.Advance
                    && x.IsValid)
                .OrderBy(x => x.Id);
        }
        
        public IEnumerable<EmployeeAdjustment> Get(int multiplier, int count)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<EmployeeAdjustment> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(EmployeeAdjustment employeeAdjustment)
        {
            _employeeAdjustmentRepository.Add(employeeAdjustment);
        }

        public void Update(EmployeeAdjustment employeeAdjustment)
        {
            _employeeAdjustmentRepository.Update(employeeAdjustment);
        }

        public void Delete(int id)
        {
            var employeeAdjustment = GetById(id);
            if (employeeAdjustment != null)
                _employeeAdjustmentRepository.Delete(employeeAdjustment);
        }

        public bool Save()
        {
            string message;
            _employeeAdjustmentRepository.Save(out message);
            return message == "Saved";
        }
    }
}
